import java.awt.Container;

import javax.swing.JButton;

import javax.swing.JPanel;

import Exceptions.InternalFailureException;
import Exceptions.InvalidOperationException;
import Exceptions.ParameterOutOfRangeException;

/**
 * 
 */

/**
 * @author Dogan
 *
 */
public class TDGamePanel extends GamePanel {

	private static final long serialVersionUID = 1L;

	private Controller contr;
	private char[][] map;
	/**
	 * @param map the map to set
	 */
	public void setMap(char[][] map) {
		this.map = map;
	}

	private Container contentPane;

	private String skin = "defaultskin";

	public TDGamePanel(GameWindow theParentWindow, Container container) {
		super(theParentWindow, container);
		try {
			registerImage("free", "images/"+skin+"/map/field.png");
			registerImage("obstcl_1", "images/"+skin+"/map/field1.png");
			registerImage("obstcl_2", "images/"+skin+"/map/field2.png");
			registerImage("obstcl_3", "images/"+skin+"/map/field3.png");
			registerImage("path", "images/"+skin+"/map/path.png");
			registerImage("spawn", "images/"+skin+"/map/spawn.png");
			registerImage("target", "images/"+skin+"/map/target.png");
			registerImage("monster_std", "images/"+skin+"/monsters/monster.png");
			registerImage("monster_slow", "images/"+skin+"/monsters/monster_black.png");
			registerImage("monster_fast", "images/"+skin+"/monsters/monster_red.png");
			registerImage("monster_immune", "images/"+skin+"/monsters/monster_blue.png");
			registerImage("monster_boss", "images/"+skin+"/monsters/monster_mult.png");
			registerImage("tower", "images/"+skin+"/towers/tower.png");
			registerImage("aoetower", "images/"+skin+"/towers/aoetower.png");
			registerImage("splashtower", "images/"+skin+"/towers/splashtower.png");
			registerImage("frosttower", "images/"+skin+"/towers/frosttower.png");
		} catch (ParameterOutOfRangeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidOperationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InternalFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		setVisible(true);
	}
	
	protected void setSkin(String s){
		skin = s;
	}

	/* (non-Javadoc)
	 * @see GamePanel#setGamePanelContents()
	 */
	@Override
	protected void setGamePanelContents() {
		// TODO Auto-generated method stub
		//map = contr.getMap();
		for(int i=0; i<map.length; i++){
			for(int j=0; j<map[0].length; j++){
				try{
					if(map[i][j] == 'S') placeEntity("spawn");
					if(map[i][j] == 'X') placeEntity("target");
					if(map[i][j] == '<' || map[i][j] == '^' || map[i][j] == '>' || map[i][j] == 'v') placeEntity("path");
					if(map[i][j] == '_') placeEntity("free");
					if(map[i][j] == '#') placeEntity("obstcl_"+(int)(Math.random()*2 +1));
					if(map[i][j] == 't') placeEntity("tower");
				} catch (ParameterOutOfRangeException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}


	public void AddTower(int x, int y, int towerid)
	{
		try {
		if(towerid==1)
				reDrawButton(x, y, "tower");
		if(towerid==2)
			reDrawButton(x, y, "aoetower");
		if(towerid==3)
			reDrawButton(x, y, "splashtower");
		if(towerid==4)
			reDrawButton(x, y, "frosttower");
		if(towerid==5)
			reDrawButton(x, y, "splashtower");
		if(towerid==0)
			reDrawButton(x, y, "free");
		
			} catch (ParameterOutOfRangeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	/**
	 * @return the controller
	 */
	public Controller getController() {
		return contr;
	}


	/**
	 * @param controller the controller to set
	 */
	public void setController(Controller controller) {
		this.contr = controller;
	}
	@Override
	protected void entityClicked(int positionX, int positionY) {
		contr.clickEntity(positionX, positionY);
	}

	/* (non-Javadoc)
	 * @see GamePanel#panelResized()
	 */
	@Override
	protected void panelResized() {
		// TODO Auto-generated method stub
		this.setSize(map.length *24, map[0].length * 24);
		this.getParentWindow().setSize(map.length * 24 + 100, map[0].length * 24);
	}
	
	

}
